package com.base.cn.platform.os.controller.web.order.goods;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.order.order.ShopCart;
import com.base.cn.platform.os.service.manage.order.goods.ShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 前台购物车Controller
 *
 * @author s.li
 * @create 2018-06-15-11:24
 */
@Controller
@RequestMapping("/web/uc/shopCar")
public class WebShopCartController extends WebBaseController {

    @Autowired
    private ShopCartService shopCartService;

    /**
     * 删除购物车商品数据
     * @param shopCartIds 购物车ID串
     * @return Map<String,Object>
     */
    @RequestMapping("/deleteShopCart")
    @ResponseBody
    public Map<String,Object> deleteShopCart(@RequestParam("shopCartIds") String shopCartIds){
        shopCartService.deleteShopCart(shopCartIds);
        return ResultUtil.SUCCESS("购物车商品删除成功");
    }

    /**
     * 修改购物车购买量
     * @param shopCartId 购物车ID
     * @param buyNum 购买量
     * @return
     */
    @RequestMapping("/updateShopCartNum")
    @ResponseBody
    public Map<String,Object> updateShopCartNum(@RequestParam("shopCartId")BigDecimal shopCartId,
                                                @RequestParam("buyNum") int buyNum){
        return shopCartService.updateShopCartNum(shopCartId,buyNum);
    }

    /**
     * 加载显示用户的购物车内容
     * @param request
     * @return Map<String,Object>
     */
    @RequestMapping("/loadShowUserShopCartList")
    @ResponseBody
    public Map<String,Object> loadShowUserShopCartList(HttpServletRequest request){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        List<ShopCart> shopList = shopCartService.findWebShowShopCartList(userId);
        return ResultUtil.SUCCESS(shopList);
    }

    /**
     * 添加购物车
     * @param shopId 商品ID
     * @param shopType 商品类型
     * @param buyNum 购买量
     * @return Map<String,Object>
     */
    @RequestMapping("/addShopCart")
    @ResponseBody
    public Map<String,Object> addShopCart(HttpServletRequest request,
                                          @RequestParam("shopId") BigDecimal shopId,
                                          @RequestParam("shopType") String shopType,
                                          @RequestParam("buyNum") int buyNum,
                                          @RequestParam(value = "shareKey",required = false) String shareKey){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return  shopCartService.addShopCart(userId,shopId,shopType,buyNum,shareKey);
    }

    /**
     * 获取购物车中商品数量
     * @param request
     */
    @RequestMapping("/ajax/queryShopCartNum")
    @ResponseBody
    public Map<String,Object> queryShopCartNum(HttpServletRequest request){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Integer count = shopCartService.queryCartShopNum(userId);
        return ResultUtil.SUCCESS(count);
    }
}
